package com.qdaily.supportlibrary.util;

import android.app.Application;
import android.graphics.Bitmap;
import android.widget.ImageView;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

/**
 * @author chencheng
 * @version 1.0
 * @time Mar 19, 20154:22:20 PM
 * @contact lovejiuwei@gmail.com
 */
public class ImageUtil {

    private static ImageUtil mImageUtil;
    private ImageLoader imageLoader = null;
    private DisplayImageOptions defaultOptions = null;
    private DisplayImageOptions roundedOptions = null;

    public static ImageUtil getInstance() {
        if (mImageUtil == null) {
            mImageUtil = new ImageUtil();
        }
        return mImageUtil;
    }

    public void init(Application application) {
        imageLoader = ImageLoader.getInstance();

        ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(application);
        config.threadPriority(Thread.NORM_PRIORITY - 2);
        config.denyCacheImageMultipleSizesInMemory();
        config.diskCacheFileNameGenerator(new Md5FileNameGenerator());
        config.diskCacheSize(50 * 1024 * 1024); // 50 MiB
        config.tasksProcessingOrder(QueueProcessingType.LIFO);
        config.writeDebugLogs(); // Remove for release app
        imageLoader.init(config.build());

        if (defaultOptions == null) {
            defaultOptions = new DisplayImageOptions.Builder().
                    cacheInMemory(true).
                    cacheOnDisk(true).
                    resetViewBeforeLoading(true).
                    considerExifParams(true).
                    bitmapConfig(Bitmap.Config.RGB_565).
                    build();
        }

        if (roundedOptions == null) {
            roundedOptions = new DisplayImageOptions.Builder().
                    cacheInMemory(true).
                    cacheOnDisk(true).
                    resetViewBeforeLoading(true).
                    considerExifParams(true).
                    bitmapConfig(Bitmap.Config.RGB_565).
                    displayer(new RoundedBitmapDisplayer(1000)).
                    build();
        }
    }

    public void clearAllCache() {
        imageLoader.clearMemoryCache();
        imageLoader.clearDiskCache();
    }

    public void displayImage(String url, ImageView imageView) {
        if (url.startsWith("/")) {
            url = "file://" + url;
        }
        imageLoader.displayImage(url, imageView, defaultOptions);
    }

    public void displayImage(String url, ImageView imageView, ImageLoadingListener imageLoadingListener) {
        imageLoader.displayImage(url, imageView, defaultOptions, imageLoadingListener);
    }

    public void displayImage(String url, ImageView imageView, DisplayImageOptions options) {
        imageLoader.displayImage(url, imageView, options);
    }

    /**
     * 网络加载图片，本地显示圆角。
     *
     * @param url
     * @param imageView
     */
    public void displayRoundedImage(String url, ImageView imageView) {
        imageLoader.displayImage(url, imageView, roundedOptions);
    }

    public void displayRoundedImage(String url, ImageView imageView, ImageLoadingListener imageLoadingListener) {
        imageLoader.displayImage(url, imageView, roundedOptions, imageLoadingListener);
    }

    /**
     * 加载本地图片需要添加file
     *
     * @param url
     * @param imageView
     */
    public void displayLocalRoundedImage(String url, ImageView imageView) {
        if (url.startsWith("/")) {
            url = "file://" + url;
        }
        imageLoader.displayImage(url, imageView, roundedOptions);
    }

    /**
     * 加载本地图片assets
     *
     * @param url
     * @param imageView
     */
    public void displayAssetsImage(String url, ImageView imageView) {
        if (url.startsWith("/")) {
            url = "assets:/" + url;
        } else {
            url = "assets://" + url;
        }
        imageLoader.displayImage(url, imageView, roundedOptions);
    }

    public void displayImage(String url, ImageView imageView, int onLoadingRes, int emptyUriRes, int onFailRes) {
        DisplayImageOptions options = new DisplayImageOptions.Builder().
                showImageOnLoading(onLoadingRes).
                showImageForEmptyUri(emptyUriRes)
                .showImageOnFail(onFailRes).
                        cacheInMemory(true).
                        cacheOnDisk(true).
                        considerExifParams(true).build();
        imageLoader.displayImage(url, imageView, options);
    }

}
